package com.yunxi.service.dubboService.impl.sys;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.yunxi.model.sys.Org;
import com.yunxi.model.sys.Pos;
import com.yunxi.service.localService.sys.OrgService;
import com.yunxi.service.localService.sys.PosService;
import com.yunxi.service.localService.sys.StaffService;
import com.yunxi.sys.pojo.OrgListVO;
import com.yunxi.sys.pojo.OrgVO;
import com.yunxi.sys.service.OrgDubboService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 组织结构 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-08
 */
/*@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)*/
public class OrgDubboServiceImpl implements OrgDubboService {

    @Autowired
    @Qualifier(value = "orgServiceImpl")
    OrgService orgService;

    @Autowired
    @Qualifier(value = "staffServiceImpl")
    StaffService staffService;

    @Autowired
    @Qualifier(value = "posServiceImpl")
    PosService posService;

    @Override
    public OrgVO getById(String id){
        Org org = orgService.getById(id);
        OrgVO orgVo = new OrgVO();
        if(org!=null){
            BeanUtils.copyProperties(org,orgVo);
        }
        return orgVo;
    }

    @Override
    public void saveOrUpdate(OrgVO orgVo) {
        Org org = new Org();
        orgVo.setOrgType(0);//0店端1厂端
        BeanUtils.copyProperties(orgVo, org);
        orgService.saveOrUpdate(org);
    }

    @Override
    public void removeById(String id) {
    orgService.removeById(id);
    }


    @Cacheable(value="orgs",key = "'org:data_'+#org_type")
    public Object findTreeListBySysType(String org_type) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return orgService.findTreeListBySysType(org_type);
    }

    @Cacheable(value="orgs",key = "'org:pid_'+#pid")
    public Object treeByPid(String pid) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return orgService.treeByPid(pid);
    }

    @Override
    public PageInfo<OrgVO> findPageByVO(OrgVO orgVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {
        PageInfo<OrgVO> pageInfo = orgService.findPageByVO(orgVO,pageNum,pageSize);
        return pageInfo;
    }

    @Override
    public Object getList() {


        JSONArray jsonArray = null;
        /* try {
            String input = FileUtils.readFileToString(new File("D://2222.json"), "UTF-8");
            input=input+"\":\"123123\"}]}";
            JSONObject jsonObject = JSONObject.parseObject(input);
            if (jsonObject != null) {
                jsonArray = jsonObject.getJSONArray("Data");
                System.out.println(jsonArray.size());

                for (int i=0;i<jsonArray.size();i++)
                {
                    JSONObject o = (JSONObject)jsonArray.get(i);
                    System.out.println(o);
//OId POIdOrgAdmin Name
                    Org org = new Org();
                    org.setOrgId(o.getString("OId"));
                    org.setPid(o.getString("POIdOrgAdmin"));
                    org.setOrgName(o.getString("Name"));
                    orgService.saveOrUpdate(org);

                    //OId POIdOrgAdmin Name
                    Org org = new Org();
                    org.setOrgId(o.getString("OId"));
                    org.setPid(o.getString("POIdOrgAdmin"));
                    org.setOrgName(o.getString("Name"));
                    orgService.saveOrUpdate(org);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

*/
        /*
        try {
            String input = FileUtils.readFileToString(new File("D://职务01.txt"), "UTF-8");
            input=input+"\"}]}";
            JSONObject jsonObject = JSONObject.parseObject(input);
            if (jsonObject != null) {
                jsonArray = jsonObject.getJSONArray("Data");
                System.out.println(jsonArray.size());

                for (int i=0;i<jsonArray.size();i++)
                {
                    JSONObject o = (JSONObject)jsonArray.get(i);
                    System.out.println(o);

                    Staff s = new Staff();
                    s.setStaffId(o.getString("OId"));
                    s.setOrgId(o.getString("OIdDepartment"));
                    s.setPositionId(o.getString("OIdJobPost"));
                    s.setStaffName(o.getString("Name"));
                    s.setAccountNo(o.getString("AccountNo"));
                    s.setGrade(o.getString("OidJobGrade"));
                    s.setLv(o.getString("OIdJobLevel"));
                    s.setEmployType(o.getString("EmployType"));
                    staffService.saveOrUpdate(s);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
*/

        try {
            String input = FileUtils.readFileToString(new File("D://职务01.txt"), "UTF-8");
            input=input+"\"}]}";
            JSONObject jsonObject = JSONObject.parseObject(input);
            if (jsonObject != null) {
                jsonArray = jsonObject.getJSONArray("Data");
                System.out.println(jsonArray.size());

                for (int i=0;i<jsonArray.size();i++)
                {
                    JSONObject o = (JSONObject)jsonArray.get(i);
                    System.out.println(o);

                    Pos s = new Pos();
                    s.setPositionId(o.getString("OId"));
                    s.setPositionCode(o.getString("Code"));
                    s.setPositionName(o.getString("Name"));
                    s.setSortNo(o.getInteger("Order"));
                    s.setStatus(o.getInteger("Status"));
                    s.setLv(o.getString("lv"));
                    posService.saveOrUpdate(s);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取组织部门列表
     * @return
     */
    @Override
    public List<OrgListVO> getOrgList() {
        List<OrgListVO> orgList = orgService.getOrgList();
        return orgList;
    }

    @Override
    public void removeByListVO(List<OrgVO> orgVoList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        orgService.removeByListVo(orgVoList);
    }

    @Override
    public List<OrgVO> listByVO(OrgVO orgVO) {
        return orgService.listByVo(orgVO);
    }

}
